﻿<div class="row g-3">
    <div class="col-12 col-sm-6">
        <Select TValue="string" Value="@TimezoneId" Items="TimezoneItems" OnValueChanged="OnTimezoneValueChanged" />
    </div>
    <div class="col-12 col-sm-6">
        <Display TValue="TimeSpan" Value="@TimezoneValue" />
    </div>
</div>

@code {
    [NotNull]
    private IEnumerable<SelectedItem>? TimezoneItems { get; set; }

    private string? TimezoneId { get; set; }

    [NotNull]
    private TimeSpan TimezoneValue { get; set; }

    private Task OnTimezoneValueChanged(string timezoneId)
    {
        TimezoneId = timezoneId;
        TimezoneValue = TimeZoneInfo.GetSystemTimeZones().First(i => i.Id == timezoneId).BaseUtcOffset;
        StateHasChanged();
        return Task.CompletedTask;
    }

    protected override void OnInitialized()
    {
        TimeZoneInfo.ClearCachedData();
        TimezoneItems = TimeZoneInfo.GetSystemTimeZones().Select(i => new SelectedItem(i.Id, i.DisplayName));
        TimezoneId = TimeZoneInfo.Local.Id;
        TimezoneValue = TimeZoneInfo.Local.BaseUtcOffset;
    }
}
